using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Windows.Media;
using Common;
using Common.Enum;
using Common.NotifyEvent;
using SExperiment.ExperimentResult;

namespace SExperiment
{
	[Serializable]
	public class SampleTargetItem : NotifyPropertyChanged
	{
		private string _TubeName;

		private int _TubeNo;

		private int _ChannelNo;

		private string _TargetName;

		private string _Dye;

		private SampleType _SampleType;

		private string _Property;

		private Hashtable _LineColor = new Hashtable();

		private Hashtable _LinePointStyle = new Hashtable();

		private string _Name;

		private TargetResult _Result;

		public string TubeName
		{
			get
			{
				return _TubeName;
			}
			set
			{
				if (!(_TubeName == value))
				{
					_TubeName = value;
					OnPropertyChanged("TubeName");
				}
			}
		}

		public int TubeNo
		{
			get
			{
				return _TubeNo;
			}
			set
			{
				if (_TubeNo != value)
				{
					_TubeNo = value;
					OnPropertyChanged("TubeNo");
				}
			}
		}

		public int ChannelNo
		{
			get
			{
				return _ChannelNo;
			}
			set
			{
				if (_ChannelNo != value)
				{
					_ChannelNo = value;
					OnPropertyChanged("ChannelNo");
				}
			}
		}

		public string TargetName
		{
			get
			{
				return _TargetName;
			}
			set
			{
				if (!(_TargetName == value))
				{
					_TargetName = value;
					OnPropertyChanged("TargetName");
				}
			}
		}

		public string Dye
		{
			get
			{
				return _Dye;
			}
			set
			{
				if (!(_Dye == value))
				{
					_Dye = value;
					OnPropertyChanged("Dye");
				}
			}
		}

		public SampleType SampleType
		{
			get
			{
				return _SampleType;
			}
			set
			{
				if (_SampleType != value)
				{
					_SampleType = value;
					OnPropertyChanged("SampleType");
				}
			}
		}

		public string Property
		{
			get
			{
				return _Property;
			}
			set
			{
				if (!(_Property == value))
				{
					_Property = value;
					OnPropertyChanged("Property");
				}
			}
		}

		private Hashtable LineColor
		{
			get
			{
				return _LineColor;
			}
			set
			{
				if (_LineColor != value)
				{
					_LineColor = value;
					RaiseLineColorChangedEvent();
				}
			}
		}

		private Hashtable LinePointStyle
		{
			get
			{
				return _LinePointStyle;
			}
			set
			{
				if (_LinePointStyle != value)
				{
					_LinePointStyle = value;
					RaiseLinePointStyleChangedEvent();
				}
			}
		}

		public string Name
		{
			get
			{
				return _Name;
			}
			set
			{
				if (!(_Name == value))
				{
					_Name = value;
					OnPropertyChanged("Name");
				}
			}
		}

		public TargetResult Result
		{
			get
			{
				return _Result;
			}
			set
			{
				if (_Result != value)
				{
					_Result = value;
					OnPropertyChanged("Result");
				}
			}
		}

		private void RaiseLineColorChangedEvent()
		{
			OnPropertyChanged("LineColor");
		}

		private void RaiseLinePointStyleChangedEvent()
		{
			OnPropertyChanged("LinePointStyle");
		}

		public SampleTargetItem(EProjectType type)
		{
			InitResult(type);
		}

		public SampleTargetItem(SampleTargetItem item)
		{
			ChannelNo = item.ChannelNo;
			Name = item.Name;
			SampleType = item.SampleType;
			Property = item.Property;
			TargetName = item.TargetName;
			TubeName = item.TubeName;
			TubeNo = item.TubeNo;
			Dye = item.Dye;
			LineColor = new Hashtable(item.LineColor);
			LinePointStyle = new Hashtable(item.LinePointStyle);
		}

		public bool HasCustomColor(EColorMode mode)
		{
			return LineColor.ContainsKey(mode);
		}

		public Color GetCustomColor(EColorMode mode)
		{
			return (Color)LineColor[mode];
		}

		public void AddColor(EColorMode mode, Color color)
		{
			if (LineColor.ContainsKey(mode))
			{
				LineColor[mode] = color;
			}
			else
			{
				LineColor.Add(mode, color);
			}
			RaiseLineColorChangedEvent();
		}

		public void DeleteColor(EColorMode mode)
		{
			if (LineColor.ContainsKey(mode))
			{
				LineColor.Remove(mode);
			}
			RaiseLineColorChangedEvent();
		}

		public bool HasCustomLinePointStyle(EColorMode mode)
		{
			return LinePointStyle.Contains(mode);
		}

		public ESeriesType GetCustomLinePointStyle(EColorMode mode)
		{
			return (ESeriesType)LinePointStyle[mode];
		}

		public void AddLinePointStyle(EColorMode mode, ESeriesType value)
		{
			if (LinePointStyle.ContainsKey(mode))
			{
				LinePointStyle[mode] = value;
			}
			else
			{
				LinePointStyle.Add(mode, value);
			}
			RaiseLinePointStyleChangedEvent();
		}

		public void DeleteLinePointStyle(EColorMode mode)
		{
			if (LinePointStyle.ContainsKey(mode))
			{
				LinePointStyle.Remove(mode);
			}
			RaiseLinePointStyleChangedEvent();
		}

		private void InitResult(EProjectType type)
		{
			if (_Result == null)
			{
				ResetResult(type);
			}
		}

		public void ResetResult(EProjectType type)
		{
			Result = ResultFactory.CreateTargetResult(type);
		}

		public virtual void SaveToStream(StreamEx stream, bool saveRawCurve = false)
		{
			stream.Write(PropertyKey.Key_SampleItem);
			List<PropertyKey> list = new List<PropertyKey>();
			List<byte[]> list2 = new List<byte[]>();
			if (!string.IsNullOrEmpty(TubeName))
			{
				list.Add(PropertyKey.Key_TubeName);
				list2.Add(BytesConverter.GetBytes(TubeName));
			}
			list.Add(PropertyKey.Key_TubeNo);
			list2.Add(BytesConverter.GetBytes(TubeNo));
			list.Add(PropertyKey.Key_ChannelNo);
			list2.Add(BytesConverter.GetBytes(ChannelNo));
			if (!string.IsNullOrEmpty(TargetName))
			{
				list.Add(PropertyKey.Key_TargetName);
				list2.Add(BytesConverter.GetBytes(TargetName));
			}
			if (!string.IsNullOrEmpty(Dye))
			{
				list.Add(PropertyKey.Key_Dye);
				list2.Add(BytesConverter.GetBytes(Dye));
			}
			list.Add(PropertyKey.Key_SampleType);
			list2.Add(BytesConverter.GetBytes((int)SampleType));
			if (!string.IsNullOrEmpty(Property))
			{
				list.Add(PropertyKey.Key_Property);
				list2.Add(BytesConverter.GetBytes(Property));
			}
			StreamEx streamEx = new StreamEx();
			if (LineColor.Count > 0)
			{
				streamEx = new StreamEx();
				streamEx.Write(LineColor.Count);
				foreach (EColorMode key in LineColor.Keys)
				{
					streamEx.Write((int)key);
					streamEx.Write(((Color)LineColor[key]/*cast due to .constrained prefix*/).ToString());
				}
				list.Add(PropertyKey.Key_HashtableLineColor);
				list2.Add(streamEx.ToArray());
				streamEx.Close();
			}
			if (LinePointStyle.Count > 0)
			{
				streamEx = new StreamEx();
				streamEx.Write(LinePointStyle.Count);
				foreach (EColorMode key2 in LinePointStyle.Keys)
				{
					streamEx.Write((int)key2);
					streamEx.Write((int)LinePointStyle[key2]);
				}
				list.Add(PropertyKey.Key_HashtableLinePointStyle);
				list2.Add(streamEx.ToArray());
				streamEx.Close();
			}
			if (saveRawCurve)
			{
				streamEx = new StreamEx();
				streamEx.Write(Result.RawCurve.CurvePoint.Count);
				foreach (Dot item in Result.RawCurve.CurvePoint)
				{
					streamEx.Write(item.X);
					streamEx.Write(item.Y);
				}
				list.Add(PropertyKey.Key_Result);
				list2.Add(streamEx.ToArray());
				streamEx.Close();
			}
			list.Add(PropertyKey.Key_SampleResult);
			streamEx = new StreamEx();
			Result.SaveToStream(streamEx);
			list2.Add(streamEx.ToArray());
			streamEx.Close();
			stream.Write(PropertyKey.Key_Index);
			stream.WriteIndex(list, list2);
			stream.Flush();
		}

		public virtual bool ReadFromStream(StreamEx stream)
		{
			int num = 0;
			PropertyKey propertyKey = stream.ReadKey();
			if (propertyKey == PropertyKey.Key_SampleItem)
			{
				propertyKey = stream.ReadKey();
				if (propertyKey == PropertyKey.Key_Index)
				{
					List<PropertyKey> keys;
					List<byte[]> dates;
					stream.ReadIndex(out keys, out dates);
					for (int i = 0; i < keys.Count; i++)
					{
						switch (keys[i])
						{
						case PropertyKey.Key_TubeName:
							TubeName = BytesConverter.BytesToString(dates[i]);
							break;
						case PropertyKey.Key_TubeNo:
							TubeNo = BytesConverter.BytesToInt(dates[i]);
							break;
						case PropertyKey.Key_ChannelNo:
							ChannelNo = BytesConverter.BytesToInt(dates[i]);
							break;
						case PropertyKey.Key_TargetName:
							TargetName = BytesConverter.BytesToString(dates[i]);
							break;
						case PropertyKey.Key_Dye:
							Dye = BytesConverter.BytesToString(dates[i]);
							break;
						case PropertyKey.Key_SampleType:
							SampleType = (SampleType)BytesConverter.BytesToInt(dates[i]);
							break;
						case PropertyKey.Key_Property:
							Property = BytesConverter.BytesToString(dates[i]);
							break;
						case PropertyKey.Key_HashtableLineColor:
						{
							StreamEx streamEx = new StreamEx();
							streamEx.Write(dates[i]);
							streamEx.Seek(0L, SeekOrigin.Begin);
							num = streamEx.ReadInt();
							LineColor.Clear();
							for (int l = 0; l < num; l++)
							{
								LineColor.Add((EColorMode)streamEx.ReadInt(), (Color)ColorConverter.ConvertFromString(streamEx.ReadString()));
							}
							streamEx.Close();
							break;
						}
						case PropertyKey.Key_HashtableLinePointStyle:
						{
							StreamEx streamEx = new StreamEx();
							streamEx.Write(dates[i]);
							streamEx.Seek(0L, SeekOrigin.Begin);
							num = streamEx.ReadInt();
							LinePointStyle.Clear();
							for (int k = 0; k < num; k++)
							{
								LinePointStyle.Add((EColorMode)streamEx.ReadInt(), (ESeriesType)streamEx.ReadInt());
							}
							streamEx.Close();
							break;
						}
						case PropertyKey.Key_Result:
						{
							StreamEx streamEx = new StreamEx();
							streamEx.Write(dates[i]);
							streamEx.Seek(0L, SeekOrigin.Begin);
							num = streamEx.ReadInt();
							for (int j = 0; j < num; j++)
							{
								Result.RawCurve.CurvePoint.Add(new Dot
								{
									X = streamEx.ReadDouble(),
									Y = streamEx.ReadDouble()
								});
							}
							streamEx.Close();
							break;
						}
						case PropertyKey.Key_SampleResult:
						{
							StreamEx streamEx = new StreamEx();
							streamEx.Write(dates[i]);
							streamEx.Seek(0L, SeekOrigin.Begin);
							Result.ReadFromStream(streamEx);
							streamEx.Close();
							break;
						}
						}
					}
					return true;
				}
				stream.Seek(-4L, SeekOrigin.Current);
				while (true)
				{
					switch (stream.ReadKey())
					{
					case PropertyKey.Key_TubeName:
						TubeName = stream.ReadString();
						break;
					case PropertyKey.Key_TubeNo:
						TubeNo = stream.ReadInt();
						break;
					case PropertyKey.Key_ChannelNo:
						ChannelNo = stream.ReadInt();
						break;
					case PropertyKey.Key_TargetName:
						TargetName = stream.ReadString();
						break;
					case PropertyKey.Key_Dye:
						Dye = stream.ReadString();
						break;
					case PropertyKey.Key_SampleType:
						SampleType = (SampleType)stream.ReadInt();
						break;
					case PropertyKey.Key_Property:
						Property = stream.ReadString();
						break;
					case PropertyKey.Key_LineColor:
						stream.ReadString();
						break;
					case PropertyKey.Key_HashtableLineColor:
					{
						num = stream.ReadInt();
						LineColor.Clear();
						for (int num2 = 0; num2 < num; num2++)
						{
							LineColor.Add((EColorMode)stream.ReadInt(), (Color)ColorConverter.ConvertFromString(stream.ReadString()));
						}
						break;
					}
					case PropertyKey.Key_HashtableLinePointStyle:
					{
						num = stream.ReadInt();
						LinePointStyle.Clear();
						for (int n = 0; n < num; n++)
						{
							LinePointStyle.Add((EColorMode)stream.ReadInt(), stream.ReadInt());
						}
						break;
					}
					case PropertyKey.Key_Result:
					{
						num = stream.ReadInt();
						for (int m = 0; m < num; m++)
						{
							Result.RawCurve.CurvePoint.Add(new Dot
							{
								X = stream.ReadDouble(),
								Y = stream.ReadDouble()
							});
						}
						break;
					}
					default:
						stream.Seek(-4L, SeekOrigin.Current);
						return true;
					}
				}
			}
			stream.Seek(-4L, SeekOrigin.Current);
			return false;
		}
	}
}
